#include "Avalon/IO/DelimitedWriter.hpp"
#include "Avalon/IO/Buffer.hpp"

using namespace Avalon;
using namespace Avalon::IO;
using namespace Avalon::Threading;

DelimitedWriter::DelimitedWriter(Carry<Writer> destination, char delimiter)
    : m_destination(destination),
      m_delimiter(delimiter) {}

DelimitedWriter::~DelimitedWriter() {}

Async<void>::Ptr DelimitedWriter::Write(const void* data, int size) {
  Buffer coalescedData(size + sizeof(m_delimiter));
  coalescedData.Write(0, data, size);
  coalescedData.Write(size, &m_delimiter, sizeof(m_delimiter));
  return m_destination->Write(coalescedData);
}

Async<void>::Ptr DelimitedWriter::Write(const Buffer& data) {
  return Write(data.GetData(), data.GetSize());
}
